package com.ykdl.member.base.service;

import org.json.JSONException;
import org.json.JSONObject;
import com.ykdl.member.base.QLog;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.advert.AdverltWebView;
import com.ykdl.member.kid.advert.AdvertListActivity;
import com.ykdl.member.kid.chat.ChatActivity;
import com.ykdl.member.kid.circle.CircleTopicActivity;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.doctor.ActionsDoctorOnlineActivity;
import com.ykdl.member.kid.doctor.CampaignInfoWebView;
import com.ykdl.member.kid.doctor.DoctorInfoActivity;
import com.ykdl.member.kid.doctor.DoctorOnlineListActivity;
import com.ykdl.member.kid.doctor.ProblemDetailsAcivity;
import com.ykdl.member.kid.doctor.ToAskDoctorActivity;
import com.ykdl.member.kid.gears.ArticleWapActivity;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.gears.TopicsSheetListActivity;
import com.ykdl.member.kid.marketcard.CardScoreWapActivity;
import com.ykdl.member.kid.marketcard.ChangeStateActivity;
import com.ykdl.member.kid.marketcard.CouponinfoWebView;
import com.ykdl.member.kid.messagebox.OfficialMessageActivity1;
import com.ykdl.member.kid.prefs.MyPrefs;
import com.ykdl.member.kid.util.ActiveStateUtil;
import com.ykdl.member.kid.widget.dialog.DialogImpl;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = "JPush";
	private Context mContext;
	@Override
	public void onReceive(Context context, Intent intent) {
		mContext=context;
        Bundle bundle = intent.getExtras();
		Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
		
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...
                        
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
        	Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
//        	processCustomMessage(context, bundle);
        
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
        	
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
            
        	//打开自定义的Activity
            try {
				goAction(context,bundle.getString(JPushInterface.EXTRA_EXTRA));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..
        	
        } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
        	boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
        	Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
        } else {
        	Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
	}

	// 打印所有的 intent extra 数据
	private static String printBundle(Bundle bundle) {
		StringBuilder sb = new StringBuilder();
		for (String key : bundle.keySet()) {
			if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
			}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
				sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
			} 
			else {
				sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
			}
		}
		return sb.toString();
	}
	
	
	
	
	 public static String getJSString(JSONObject obj, String name) {
			try {
				return obj.getString(name);
			} catch (Exception ee) {

			}
			return "";
		}
	 public void goAction(Context context,String str) throws JSONException {
		 JSONObject obj = new JSONObject(str);
		 String paction = getJSString(obj, "action");
		 String open_type = getJSString(obj, "open_type");
			if(paction==null){
				return;
			}

			if(TextUtils.isEmpty(paction))return;
			if (open_type.equals("2")) {
				Intent intent = new Intent(); 
				intent.setAction("android.intent.action.VIEW");    
			        Uri content_url = Uri.parse(paction);   
			        intent.setData(content_url); 
			        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			        context.startActivity(intent);
			}else {
				if (paction.startsWith("http:")) {
					Intent intent = new Intent(mContext,AdverltWebView.class);
					intent.putExtra(KidAction.INTENT_URL, paction+"");
					context.startActivity(intent);
				}else {
					String[] end = paction.split("\\//")[1].split("\\/");
					Intent intent = null;
					 if (paction.contains("subject")) {//进帖子详情
						 intent =new Intent(mContext,TopicAndResultActivity.class);
						 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						 intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID,end[2]);
						 intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, end[1]);
					} else if (paction.contains("lottery_game")) {//游戏
						intent = new Intent(mContext, CouponinfoWebView.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra(KidAction.LOTTERY_ID, end[1]);
						if (end[2].equals("1")) {
							intent.putExtra(KidAction.URL_TYPE, 5);
						}else{
							intent.putExtra(KidAction.URL_TYPE, 6);
						}
					} else if (paction.contains("exchange_charitable")){//积分慈善
						intent = new Intent(mContext, CardScoreWapActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra(KidAction.URL_TYPE, 3);
						intent.putExtra(KidAction.POINT_TYPE_ID, end[1]);
					} else if (paction.contains("exchange_lottery")){//积分抽奖
						intent = new Intent(mContext, CardScoreWapActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra(KidAction.URL_TYPE, 2);
						intent.putExtra(KidAction.POINT_TYPE_ID, end[1]);
						
					} else if(paction.contains("message_read")){//普通专题
						intent = new Intent(mContext, OfficialMessageActivity1.class);
						intent.putExtra(KidAction.MESSAGE_ID,  end[1]);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					}else if(paction.contains("chat_actor_conversation")){//聊天
						intent = new Intent(mContext, ChatActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra(KidAction.COOPERATIVE_CORP_ID, end[1]);
					}else if(paction.contains("consume_status")){//医生在线
						intent = new Intent(mContext, ChangeStateActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//						ActiveStateUtil.getInstance(context).gogoActionsDoctorOnlineByStatus(end[2], progress);
					}/*else if(paction.contains("articles")){//医生简介
						intent = new Intent(mContext, ArticleWapActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							intent.putExtra("article_id", end[1]);
					}else if(paction.contains("activities")){//医生简介
						intent = new Intent(mContext, AdvertListActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					}else if(paction.contains("official")){//医生简介
						intent = new Intent(mContext, OfficialMessageActivity1.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra("message_type_id", 2+"");
					}else if(paction.contains("profession_topic")){//专家坐诊帖子页 
						intent = new Intent(mContext, ProblemDetailsAcivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra(KidAction.DOCTOR_TOPICI_DD, end[2]);
				}else if(paction.contains("profession")){//专家坐诊活动详情页
					if (end[2].equals("0")) {
						intent = new Intent(mContext,CampaignInfoWebView.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, end[1]);
					}else if(end[2].equals("1")){
						intent = new Intent(mContext, ActionsDoctorOnlineActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, end[1]);
					}else if(end[2].equals("2")){
						intent = new Intent(mContext, ActionsDoctorOnlineActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, end[1]);
						intent.putExtra(KidAction.IS_GONE_BT, false);
					}
				}*/
					 if(intent ==null)return;
					 context.startActivity(intent);
					}
			}
			
			}
	 
	//send msg to MainActivity
}
